package com.book.vo;

import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;

/**
 * @Author 李锦程
 * @Date 2021/3/28 8:01 下午
 * @Package com.book.util
 * @Version 1.0
 */
@Data
public class ResponseVo<T> implements Serializable {
    private static final long serialVersionUID = 5147285805394771156L;
    public int code;
    public String message;
    public T data;
    public static final String MSG_SUCCESS = "success";
    public static final int CODE_SUCCESS = 0;
    public static final int CODE_ERROR = 1;

    public ResponseVo() {
        this.code = CODE_SUCCESS;
        this.message = MSG_SUCCESS;
        this.data = null;
    }

    public ResponseVo(T data) {
        this.code = CODE_SUCCESS;
        this.message = MSG_SUCCESS;
        this.data = data;

    }

    public ResponseVo(String message) {
        this.code = CODE_ERROR;
        this.message = message;
        this.data = null;
    }

    public ResponseVo(int code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
    }

    public ResponseVo(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ResponseVo<T> errRest(String message) {
        ResponseVo<T> responseVo = new ResponseVo<>(message);
        return responseVo;
    }

    public static <T> ResponseVo<T> errRest(String message, T data) {
        return new ResponseVo<>(CODE_ERROR, message, data);
    }

    public static <T> ResponseVo<T> successResult(T data) {
        return new ResponseVo<>(data);
    }

    public static <T> ResponseVo<T> successResultNotData() {
        return new ResponseVo<>();
    }

    public static <T> ResponseVo<T> successResultNotData(String msg) {
        return new ResponseVo<>(CODE_SUCCESS, msg);
    }

    public  boolean isSuccess() {
        return this.code == CODE_SUCCESS;
    }

    public boolean isFail() {
        return this.code != CODE_SUCCESS;
    }
}

